package com.kang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kang.entity.SysMenu;
import com.kang.entity.SysRoleMenu;
import com.kang.entity.SysUser;
import com.kang.mapper.SysMenuMapper;
import com.kang.mapper.SysRoleMenuMapper;
import com.kang.service.SysMenuService;
import com.kang.util.MenuHelper;
import com.kang.util.ThreadLocalUtil;
import com.kang.vo.AssginMenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.swing.MenuItemLayoutHelper;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysMenu> findNodes() {
        List<SysMenu> menuList = sysMenuMapper.selectList(null);
        SysUser currentUser = ThreadLocalUtil.getCurrentUser();
        return MenuHelper.buildTree(menuList);
    }

    @Override
    public Integer saveMenu(SysMenu sysMenu) {
        return sysMenuMapper.insert(sysMenu);
    }

    @Override
    public SysMenu findNodeById(Integer id) {
        return sysMenuMapper.selectById(id);
    }

    @Override
    public Integer updateMenu(SysMenu sysMenu) {
        return sysMenuMapper.updateById(sysMenu);
    }

    @Override
    public Integer deleteMenuById(Integer id) throws Exception {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<SysMenu> sysMenuList = sysMenuMapper.selectList(wrapper);
        if (sysMenuList.size() > 0) {
            //抛出异常后会走catch里面
            throw new Exception("请先删除子菜单");
        }
        return sysMenuMapper.deleteById(id);
    }

    @Override
    public List<SysMenu> toAssignByRoleId(Integer roleId) {
        QueryWrapper<SysMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<SysMenu> sysMenuList = baseMapper.selectList(wrapper);
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_id",roleId);
        List<Integer> menuIdList = sysRoleMenuMapper.selectList(queryWrapper).stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
        for (SysMenu sysMenu : sysMenuList) {
            for (Integer menuId : menuIdList) {
                if (sysMenu.getId().equals(menuId)) {
                    sysMenu.setSelect(true);
                }
            }
        }

        return MenuHelper.buildTree(sysMenuList);
    }

    @Override
    public Integer doAssignByRoleId(AssginMenuVo assginMenuVo) {
        QueryWrapper<SysRoleMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("role_id",assginMenuVo.getRoleId());
        sysRoleMenuMapper.delete(wrapper);
        SysRoleMenu sysRoleMenu = new SysRoleMenu();
        sysRoleMenu.setRoleId(assginMenuVo.getRoleId());
        for (Integer menuId : assginMenuVo.getMenuIdList()) {
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenuMapper.insert(sysRoleMenu);
        }
        return 1;
    }
}
